package com.macro.mall.ums.service.address.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.exception.BusinessException;
import com.macro.mall.ums.domain.MemberAddress;
import com.macro.mall.ums.entity.MemberAddressEntity;
import com.macro.mall.ums.mapper.MemberAddressMapper;
import com.macro.mall.ums.service.address.MemberAddressService;
import com.macro.mall.ums.service.address.bo.InsertMemberAddressInput;
import com.macro.mall.ums.service.address.bo.PagedQueryMemberAddressInput;
import com.macro.mall.ums.service.address.bo.UpdateMemberAddressInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员收货地址Service
 *
 * @author xuyanjun
 * @date 2019-12-15
 */
@Slf4j(topic = "MemberAddressServiceImpl")
@Service
public class MemberAddressServiceImpl implements MemberAddressService {

    @Autowired
    private MemberAddressMapper memberAddressMapper;

    /**
     * 新增会员收货地址
     *
     * @param input input
     * @return 新增id
     */
    @Override
    public long insert(InsertMemberAddressInput input) {
        MemberAddressEntity memberAddressEntity = new MemberAddressEntity();
        memberAddressEntity
                .setMemberId(input.getMemberId())
                .setName(input.getName())
                .setPhoneNumber(input.getPhoneNumber())
                .setDefaultStatus(input.getDefaultStatus())
                .setPostCode(input.getPostCode())
                .setAreaCode(input.getAreaCode())
                .setProvince(input.getProvince())
                .setCity(input.getCity())
                .setRegion(input.getRegion())
                .setDetailAddress(input.getDetailAddress());

        memberAddressMapper.insert(memberAddressEntity);
        return memberAddressEntity.getId();
    }

    /**
     * 修改会员收货地址
     *
     * @param input input
     */
    @Override
    public void update(UpdateMemberAddressInput input) {
        MemberAddressEntity memberAddressEntity = memberAddressMapper.selectById(input.getId());
        if (memberAddressEntity == null) {
            throw new BusinessException(String.format("未能根据Id:%s找到指定的会员收货地址。", input.getId()));
        }

        memberAddressEntity = new MemberAddressEntity();
        memberAddressEntity
                .setId(input.getId())
                .setName(input.getName())
                .setPhoneNumber(input.getPhoneNumber())
                .setDefaultStatus(input.getDefaultStatus())
                .setPostCode(input.getPostCode())
                .setAreaCode(input.getAreaCode())
                .setProvince(input.getProvince())
                .setCity(input.getCity())
                .setRegion(input.getRegion())
                .setDetailAddress(input.getDetailAddress());


        memberAddressMapper.updateById(memberAddressEntity);
    }

    /**
     * 删除会员收货地址
     *
     * @param id id
     */
    @Override
    public void delete(long id) {
        memberAddressMapper.deleteById(id);
    }

    /**
     * 根据Id获取会员收货地址
     *
     * @param id id
     * @return 会员收货地址
     */
    @Override
    public MemberAddressEntity getById(long id) {
        return memberAddressMapper.selectById(id);
    }

    /**
     * 根据会员Id获取会员收货地址
     *
     * @param memberId 会员id
     * @return 会员收货地址集合
     */
    @Override
    public List<MemberAddressEntity> listByMemberId(long memberId) {
        LambdaQueryWrapper<MemberAddressEntity> queryWrapper = Wrappers.lambdaQuery();

        queryWrapper.eq(MemberAddressEntity::getMemberId, memberId);

        return memberAddressMapper.selectList(queryWrapper);
    }

    /**
     * 根据批量Id获取会员收货地址
     *
     * @param batchIds 批量ids
     * @return 会员收货地址集合
     */
    @Override
    public List<MemberAddressEntity> listByBatchIds(List<Long> batchIds) {
        if (batchIds == null || batchIds.isEmpty()) {
            return new ArrayList<>();
        }
        return memberAddressMapper.selectBatchIds(batchIds);
    }

    /**
     * 分页查询
     *
     * @param input input
     * @return 分页的会员收货地址
     */
    @Override
    public Page<MemberAddressEntity> pagedQuery(PagedQueryMemberAddressInput input) {
        LambdaQueryWrapper<MemberAddressEntity> queryWrapper = Wrappers.lambdaQuery();

        queryWrapper.eq(input.getMemberId() != null, MemberAddressEntity::getMemberId, input.getMemberId());

        PageHelper.startPage(input.getPageIndex(), input.getPageSize());

        return (Page<MemberAddressEntity>) memberAddressMapper.selectList(queryWrapper);
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param memberAddressEntity 实体
     * @return memberAddress
     */
    @Override
    public MemberAddress entityToDomain(MemberAddressEntity memberAddressEntity) {
        if (memberAddressEntity == null) {
            return null;
        }

        MemberAddress memberAddress = new MemberAddress();
        memberAddress
                .setId(memberAddressEntity.getId())
                .setMemberId(memberAddressEntity.getMemberId())
                .setName(memberAddressEntity.getName())
                .setPhoneNumber(memberAddressEntity.getPhoneNumber())
                .setDefaultStatus(memberAddressEntity.getDefaultStatus())
                .setPostCode(memberAddressEntity.getPostCode())
                .setAreaCode(memberAddressEntity.getAreaCode())
                .setProvince(memberAddressEntity.getProvince())
                .setCity(memberAddressEntity.getCity())
                .setRegion(memberAddressEntity.getRegion())
                .setDetailAddress(memberAddressEntity.getDetailAddress())
        ;

        return memberAddress;
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param memberAddressEntities 实体
     * @return memberAddresss
     */
    @Override
    public List<MemberAddress> entityToDomain(List<MemberAddressEntity> memberAddressEntities) {
        List<MemberAddress> result = new ArrayList<>();
        if (memberAddressEntities == null) {
            return result;
        }

        for (MemberAddressEntity memberAddressEntity : memberAddressEntities) {
            MemberAddress memberAddress = entityToDomain(memberAddressEntity);
            result.add(memberAddress);
        }
        return result;
    }

}
